
var gParam;

function setImageFile(param) {
	gParam = param;
	$("#"+param.targetId).attr("onclick", "fileClear(this)");
	$("#"+param.targetId).attr("onchange", "previewImg(gParam)");
	if(param.bigPreviewId != null && param.bigPreviewId != "undefined") {
		$("#"+param.previewId).attr("onmouseover", "mousePreview(this,'"+param.bigPreviewId+"')");
	}
}


function previewImg(param) {
	var targetObj = document.getElementById(param.targetId);
	var previewId = param.previewId;
	var bigPreviewId = param.bigPreviewId;
	var previewWidth = param.previewWidth;
	var previewHeight = param.previewHeight;
	var bigPreviewWidth = param.bigPreviewWidth;
	var bigPreviewHeight = param.bigPreviewHeight;
	var sizeId = param.sizeId;
	
    var preview = document.getElementById(previewId);
    var ua = window.navigator.userAgent;
    
    if (ua.indexOf("MSIE") > -1) {
    	var src;
    	
    	if(navigator.appVersion.indexOf("MSIE 1") > -1) {
        	previewNotIE(param);
        	return;
    	}else if(navigator.appVersion.indexOf("MSIE 9") > -1) {
    		return;
    		
    	}else{
    		src = document.selection.createRange().text;
    	}
    	
        targetObj.select();
        
        var ie_preview_error = document
                    .getElementById("ie_preview_error_" + previewId);

        if (ie_preview_error) {
            preview.removeChild(ie_preview_error);
        }

        var img = document.getElementById(previewId);
        img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"
                    + src + "', sizingMethod='scale')";
            
//        var hiddenPreview = document.getElementById("hiddenP");
        var hiddenPreview = $("<div/>", {
        	"style": "display:none;",
        	"id": "hiddenP"
        });
        $(hiddenPreview).css("width", previewWidth).css("height", previewHeight);
        $("body").append(hiddenPreview);
        
        hiddenPreview.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"
            + src + "', sizingMethod='image')";
        
        if(bigPreviewId != null && bigPreviewId != "undefined") {
        	$("#"+bigPreviewId).css("filter", img.style.filter ).css("width", bigPreviewWidth).css("height", bigPreviewHeight);
        }
        
        $("#hiddenP").css("width", "1px");
        $("#hiddenP").css("height", "1px");
        
        if(sizeId != null && sizeId != "undefined") {
        	$("#"+sizeId).html( $("#hiddenP").css("width") + " x " + $("#hiddenP").css("height") );
        }
            
    } else {
    	previewNotIE(param);
    }
}
	

//IE 외 브라우저 처리.
function previewNotIE(param) {
	var targetObj = document.getElementById(param.targetId);
	var previewId = param.previewId;
	var bigPreviewId = param.bigPreviewId;
	var previewWidth = param.previewWidth;
	var previewHeight = param.previewHeight;
	var bigPreviewWidth = param.bigPreviewWidth;
	var bigPreviewHeight = param.bigPreviewHeight;
	var sizeId = param.sizeId;
	
	var preview = document.getElementById(previewId);
	var files = targetObj.files;

	for ( var i = 0; i < files.length; i++) {

		var file = files[i];

		var imageType = /image.*/;
		if (!file.type.match(imageType))
			continue;

		var prevImg = document.getElementById("img_" + previewId);
		if (prevImg) {
			preview.removeChild(prevImg);
		}

		var img = document.createElement("img");
		img.id = "img_"+previewId;
		img.classList.add("obj");
		img.file = file;
		preview.appendChild(img);
		$("#img_"+previewId).css("width", previewWidth).css("height", previewHeight);

		if(bigPreviewId != null && bigPreviewId != "undefined") {
			$("#"+bigPreviewId).html("<img id='bigImg'/>");
			$("#bigImg").css("width", bigPreviewWidth).css("height", bigPreviewHeight);
		}

		if (window.FileReader) {
			var reader = new FileReader();
			reader.onloadend = (function(aImg) {
				return function(e) {
					aImg.src = e.target.result;
					$("#bigImg").attr("src", e.target.result);
					
					if(sizeId != null && sizeId != "undefined"){
						var imageForSize = new Image();
						imageForSize.onload = function() {
							$("#"+sizeId).html(this.width + " x " + this.height);
						};
						imageForSize.src = e.target.result;
					}
				};
			})(img);
			reader.readAsDataURL(file);
		} else {
			if (!document.getElementById("sfr_preview_error_" + previewId)) {
				var info = document.createElement("p");
				info.id = "sfr_preview_error_" + previewId;
				info.innerHTML = "not supported FileReader";
				preview.insertBefore(info, null);
			}
		}
	}
}

function mousePreview( previewObj, bigPreviewId ) {
	var ua = window.navigator.userAgent;
	if ( ua.indexOf("MSIE") > -1 ) {
		if(navigator.appVersion.indexOf("MSIE 1") > -1) {
			$("#"+bigPreviewId).find("#bigImg").attr("src", $(previewObj).attr("src") );
		} else {
			$("#"+bigPreviewId).css("filter", $("#preview"+num ).css("filter") );
		}
		
	} else {
		$("#"+bigPreviewId).find("#bigImg").attr("src", $(previewObj).attr("src") );
	}
}

function fileClear( targetObj ) {
	var apps = navigator.appVersion;
	
	if ( apps.indexOf("MSIE") > -1 ) {
		if(apps.indexOf("MSIE 1") > -1) {
			$(targetObj).val("");
			return;
		}
		targetObj.select();
		document.selection.clear();
	} else {
		$(targetObj).val("");
	}
}